package com.mmall.concurrency.example.atomic;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @Author: liqiang
 * @description: Oh my god!
 * @Date: Created in 22:26 2018/4/4
 * @Modified:
 */
@Slf4j
public class AtomicExcemple5 {

    private static AtomicIntegerFieldUpdater<AtomicExcemple5> atf = AtomicIntegerFieldUpdater.newUpdater(AtomicExcemple5.class,"count");


    @Getter
    private volatile int count  = 20;//必须是volatile修饰的非static 不能是Integer



    public static void main(String[] args) throws Exception {
        AtomicExcemple5 AtomicExcemple5 = new AtomicExcemple5();

        if (atf.compareAndSet(AtomicExcemple5,20,200)){
            log.info(" updata success , {}",AtomicExcemple5.getCount());
        }
        if (atf.compareAndSet(AtomicExcemple5,20,250)) {
            log.info("update success , {}",AtomicExcemple5.getCount());
        }else {
            log.error("update fail , {}", AtomicExcemple5.getCount());
        }

    }
}
